package com.winchain.chainbill.receivables.biz.query.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.feign.PubUserFeign;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.user.po.PubUser;
import com.winchain.chainbill.receivables.base.dao.ArRecordDao;
import com.winchain.chainbill.receivables.base.po.ArRecord;
import com.winchain.chainbill.receivables.biz.query.service.ReceivablesQueryService;
import com.winchain.chainbill.receivables.biz.query.vo.ArRecordQueryVo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("receivablesQueryService")
public class ReceivablesQueryServiceImpl
  extends PersistentServiceImpl<ArRecord>
  implements ReceivablesQueryService
{
  @Autowired
  ArRecordDao arRecordDao;
  @Autowired
  PubUserFeign userFeign;
  
  @Pagination
  public List<ArRecord> selectRecordForImptRev(PageLimit Limit, ArRecordQueryVo arRecord)
  {
    if (StringUtil.notEmpty(arRecord.getSellerName())) {
      arRecord.setSellerName(StringUtil.getParameterLike(arRecord.getSellerName()));
    }
    arRecord.setBuyerId(CommonDataUtil.getCurEntId());
    String[] arStates = { "0", "I" };
    arRecord.setArStates(arStates);
    return this.arRecordDao.selectRecords(arRecord);
  }
  
  public ArRecord selectRecord(String arId)
  {
    ArRecord record = (ArRecord)selectByKey(arId);
    PubUser user = this.userFeign.selectByKey(record.getCreatorId());
    if (null != user) {
      record.setCreatorName(user.getUserName());
    }
    return record;
  }
  
  @Pagination
  public List<ArRecord> selectRecordForMod(PageLimit Limit, ArRecordQueryVo arRecord)
  {
    if (StringUtil.notEmpty(arRecord.getSellerName())) {
      arRecord.setSellerName(StringUtil.getParameterLike(arRecord.getSellerName()));
    }
    arRecord.setBuyerId(CommonDataUtil.getCurEntId());
    return this.arRecordDao.selectRecords(arRecord);
  }
  
  @Pagination
  public List<ArRecord> selectRecordForModRev(PageLimit Limit, ArRecordQueryVo arRecord)
  {
    if (StringUtil.notEmpty(arRecord.getSellerName())) {
      arRecord.setSellerName(StringUtil.getParameterLike(arRecord.getSellerName()));
    }
    arRecord.setBuyerId(CommonDataUtil.getCurEntId());
    arRecord.setArState("3");
    return this.arRecordDao.selectRecordForModRev(arRecord);
  }
  
  @Pagination
  public List<ArRecord> selectRecordForModAprv(PageLimit Limit, ArRecordQueryVo arRecord)
  {
    if (StringUtil.notEmpty(arRecord.getSellerName())) {
      arRecord.setSellerName(StringUtil.getParameterLike(arRecord.getSellerName()));
    }
    String[] arStates = { "4", "M" };
    arRecord.setArStates(arStates);
    return this.arRecordDao.selectRecordForAprv(arRecord);
  }
  
  public ArRecord selectRecordMod(String arId, String modId)
  {
    ArRecord record = this.arRecordDao.selectRecordMod(arId, modId);
    
    PubUser user = this.userFeign.selectByKey(record.getCreatorId());
    if (null != user) {
      record.setCreatorName(user.getUserName());
    }
    return record;
  }
}
